ToothGrowth data set contains the results from an experiment to compare the effect of two supplements on tooth growth in 60 guinea pigs. A data frame with 60 observations on 3 variables.
| len | supp | dose |
|---|---|---|
| 4.2 | VC | 0.5 |
| 11.5 | VC | 0.5 |
| 7.3 | VC | 0.5 |
| 5.8 | VC | 0.5 |
| 6.4 | VC | 0.5 |
| 10.0 | VC | 0.5 |
tg_vc <- ToothGrowth[ToothGrowth$supp == "VC", ]
tg_oj <- ToothGrowth[ToothGrowth$supp == "OJ", ]
plot(tg_vc$dose, tg_vc$len,
main = "Tooth Growth by Supplement Type",
xlab = "Dose (mg/day)",
ylab = "Tooth length",
pch = 19, # solid circle
col = "blue")
points(tg_oj$dose, tg_oj$len, pch = 19, col = "red")
legend("bottomright", legend = c("VC", "OJ"), col = c("blue", "red"), pch = 19)plot function vs. points functionplot function creates a new plotpoints function adds points to an existing plotlines function adds lines to an existing plot
lines function is like points(type = "l")Here len ~ supp is a formula that tells R to plot len on the y-axis and supp on the x-axis.
Input for boxplot function can be a formula, a matrix, data frame, or a list.
stripchart functionStripchart is a one-dimensional scatter plot that represents the distribution of a continuous variable for different levels of a categorical variable. It can be used for similar data like boxplot.
stripchart functionWhat is points overlap? We can use jitter to spread the points out.
counts <- table(mtcars$gear) # frequency table
par(mfrow = c(1,2))
barplot(counts,
main = "Bar Chart of Gears",
xlab = "Number of Gears",
ylab = "Frequency",
col = "lightgreen")
# multiple bar plots
counts <- table(mtcars$am, mtcars$gear)
barplot(counts,
main = "Bar Chart of Gears by Transmission",
xlab = "Number of Gears",
ylab = "Frequency",
col = c("lightblue", "lightgreen"))Function hist takes a vector of values and computes a histogram.
In default settings, hist calculates the number of bins automatically. You can specify the number of bins using the breaks argument.
par(mfrow = c(2,3))
plot(x,y, main='default plot')
plot(x,y, col="#FF0000", main ="col set point color")
plot(1:20, col=rainbow(20), main ='col is a vector os colors')
plot(1:20, pch=1:20, main = "pch set point shape")
plot(x,y, cex = 3, main = "cex set point size")
plot(x,y, main = "plot with lines and grid")
abline(0, 2) # line with intercept 0 and slope 1
grid()par(mfrow = c(2,3))
plot(x,y, main='default plot', type='b')
plot(x,y, xlim=c(0, 30), ylim=c(0, 20), type='b', main = "setting limits of x and y axes")
plot(x,y, log='y', type='b', main = "log scale on y-axis")
plot(x,y, log='x', type='b', main = "log scale on x-axis")
plot(x,y, log='xy', type='b', main = "log scale on both axes")plot(x, y, pch = 19, col = "blue")
points(x + 0.5, y + 0.5, col = "red", pch = 17)
# Add a legend to differentiate groups
legend("topleft",
legend = c("Group 1", "Group 2"),
col = c("blue", "red"),
pch = c(19, 17))
# Add a mathematical annotation using expression()
mtext(expression(alpha + beta == gamma), side = 3, cex =2)Regular grid layout using par function
par(mfrow = c(2, 3)) # vector specifying the number of rows and columns
plot(x, y, main = "plot 1", col = "1")
plot(x, y, main = "plot 2", col = "2")
plot(x, y, main = "plot 3", col = "3")
plot(x, y, main = "plot 4", col = "4")
plot(x, y, main = "plot 5", col = "5")
plot(x, y, main = "plot 6", col = "6")Custom grid layout using layout function
Custom grid layout using layout function
png for bitmap graphicspng("outputs/plot1.png", width = 800, height = 600) # by defaults units are pixels
# whatever is plotted between `png` call and `dev.off` will be saved in the file
plot(x, y, main = "Scatter Plot", col = "blue")
dev.off()png
2
jpeg, bmp, tiffpar or layout functions.pdf for vector graphics - also suitable for multi-page documentssvg for vector graphics Vector graphics is suitable if you want to do additional editing in a vector graphics editor like Inkscape or Adobe Illustrator.par functionpar function is used to set large number of graphical parameterspar(mfrow = c(2, 2)) will create a 2x2 grid layoutpar function can be used to set many graphical parameters like margins, font size, font family, etc.
par(mar = c(5, 4, 4, 2) + 0.1) will set the margins of the plotpar function can be used to set graphical parameters for text, lines, points, etc.
par(cex = 1.5) will set the size of text to 1.5 times the default sizepar function can be used to set graphical parameters for colors, line types, line widths, etc.
par(lty = 2) will set the line type to dashed?par for more detailsplot(x, y)plot(x, y, type = "l")boxplot(len ~ supp, data = ToothGrowth)barplot(counts)hist(x)stripchart(len ~ supp, data = ToothGrowth)png(), pdf()par()par(mfrow = c(2, 2)), layout()